using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MediaWikiEngine.Domain;
using MediaWikiEngine;
using MediaWikiEngine.Command;
using System.IO;

namespace InteractiveCommandTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = _username.Text;
            string password = _password.Text;

            MediaWikiEngine.Domain.WikiConnection.WikiEndpoint.BaseAddress = _basePath.Text;

            InteractiveCommand cmd = new InteractiveCommand(new WikiConnection(_hostname.Text, new RequestLimiter(new TimeSpan(0,0,0,0))));
            LoginResult login = cmd.Login(username, password);
            if (true == login.LoginSuccess)
            {
                GetPageResult getPage = cmd.GetPage("Main", "Test");
                WikiPage testPage = getPage.Page;
                if (false == testPage.Exists)
                {
                    MessageBox.Show("Page does not exist");
                }
                else
                {
                    testPage.WikiMarkup += string.Format("\r\n==Test section==\r\n\r\nAdded at {0}\r\n", DateTime.Now);
                    cmd.SavePage(testPage, "Timestamped edit");
                    getPage = cmd.GetPage("Main", "Test");
                    testPage = getPage.Page;
                    MessageBox.Show(testPage.WikiMarkup, "reget");
                }
            }
            else
            {
                MessageBox.Show("Couldn't log in");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK != openFileDialog1.ShowDialog())
                return;

            string filename = openFileDialog1.FileName;

            string username = _username.Text;
            string password = _password.Text;

            MediaWikiEngine.Domain.WikiConnection.WikiEndpoint.BaseAddress = _basePath.Text;

            InteractiveCommand cmd = new InteractiveCommand(new WikiConnection(_hostname.Text, new RequestLimiter(new TimeSpan(0, 0, 0, 0))));
            LoginResult login = cmd.Login(username, password);
            if (true == login.LoginSuccess)
            {
                //see if image exists
                string title = string.Format("Image:{0}", Path.GetFileName(filename));
                GetPageResult getPage = cmd.GetPage("Main", title);
                if (true == getPage.Page.Exists)
                {
                    MessageBox.Show("page already exists!");
                    return;
                }

                //if not, upload the image
                UploadFileResult uploadFile = cmd.UploadFile(filename, "image/jpeg", Path.GetFileName(filename), "test image","self|GFDL|cc-by-sa-2.5,2.0,1.0|Own work, copyleft (Multi-license GFDL, CC-BY-SA 1.0 - 2.5)");

                //reget the image page to confirm
                getPage = cmd.GetPage("Main", title);
                MessageBox.Show(getPage.Page.WikiMarkup, "reretrieve");
            }
            else
            {
                MessageBox.Show("Couldn't log in");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string username = _username.Text;
            string password = _password.Text;

            MediaWikiEngine.Domain.WikiConnection.WikiEndpoint.BaseAddress = _basePath.Text;

            InteractiveCommand cmd = new InteractiveCommand(new WikiConnection(_hostname.Text, new RequestLimiter(new TimeSpan(0, 0, 0, 0))));
            LoginResult login = cmd.Login(username, password);
            if (true == login.LoginSuccess)
            {
                GetPageResult getPage = cmd.GetPage("Main", "Test");
                WikiPage testPage = getPage.Page;

                MessageBox.Show("Current categories: " + string.Join(",", testPage.Categories.ToArray()));

                testPage.Categories.Add(DateTime.Now.Ticks.ToString());
                testPage.ReplaceCategories();

                cmd.SavePage(testPage, "Category add");

                getPage = cmd.GetPage("Main", "Test");
                testPage = getPage.Page;
                MessageBox.Show(testPage.WikiMarkup, "reget");
            }
            else
            {
                MessageBox.Show("Couldn't log in");
            }
        }
    }
}